#include "stdafx.h"
#include "TextView.h"

TextView::TextView()
: m_hFont(0)
, m_dwColor(0)
{

}

TextView::~TextView()
{
	if (m_hFont)
		DeleteObject(m_hFont);
}

void TextView::SetText(const wchar_t *text, DWORD dwColor)
{
	m_text = text;
	m_dwColor = dwColor;
	m_bTransparent = true;

	SIZE sz = {0};
	GetSize(&sz);
	if (sz.cx == 0 || sz.cy == 0) {
		if (m_hFont) {
			HDC hdc = CreateCompatibleDC(NULL);
			HGDIOBJ hOld = SelectObject(hdc, m_hFont);

			RECT rcText = {0, 0, 8888, 8888};
			DrawText(hdc, m_text.c_str(), m_text.length(), &rcText, DT_CALCRECT|DT_SINGLELINE);

			SelectObject(hdc, hOld);

			sz.cx = rcText.right;
			sz.cy = rcText.bottom;
			SetSize(&sz);
			DeleteObject(hdc);
		}
	}
}

void TextView::Render( HDC hdc, const RECT *lpRect )
{
	SetBkMode(hdc, TRANSPARENT);
	SetTextColor(hdc, m_dwColor);

	if (m_hFont) {
		HGDIOBJ hOld = SelectObject(hdc, m_hFont);

		RECT rcText = {0};
		GetRect(&rcText);
		DrawText(hdc, m_text.c_str(), m_text.length(), &rcText, DT_SINGLELINE);

		SelectObject(hdc, hOld);
	}
}

void TextView::SetFont(const wchar_t *text, int nSize, int nWeight)
{
	LOGFONT logFont;
	memset(&logFont, 0, sizeof(LOGFONT));
	logFont.lfCharSet = DEFAULT_CHARSET;
	logFont.lfHeight = nSize;
	logFont.lfWeight = nWeight;
	logFont.lfQuality = CLEARTYPE_QUALITY;//NONANTIALIASED_QUALITY;

	wcscpy(logFont.lfFaceName, text);
	m_hFont = ::CreateFontIndirect(&logFont);
}